package org.o14x.alpha.ui.coolbar;

import java.io.File;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.o14x.alpha.domain.ExplorerTab;
import org.o14x.alpha.ui.controller.UIController;
import org.o14x.alpha.util.Messages;

/**
 * Contribution item for the address bar.
 * 
 * @author Olivier DANGREAUX
 */
public class AddressBarContributionItem extends ToolBarContributionItem implements IContributionItem {
	/**
	 * The address combobox.
	 */
	private Combo addressCombo;

	/**
	 * The UIController.
	 */
	private UIController uiController;

	/**
	 * Creates a new instance of AddressBarContributionItem.
	 * 
	 * @param uiController The UIController.
	 */
	public AddressBarContributionItem(UIController uiController) {
		this.uiController = uiController;
		this.uiController.getUi().setAddressBarContributionItem(this);
	}

	/**
	 * {@inheritDoc}
	 */
	public void fill(CoolBar coolBar, int index) {
		Control box = createBarComposite(coolBar);
		CoolItem coolItem;
		int flags = SWT.DROP_DOWN;
		if (index >= 0) {
			coolItem = new CoolItem(coolBar, flags, index);
		} else {
			coolItem = new CoolItem(coolBar, flags);
		}
		coolItem.setData(this);
		coolItem.setControl(box);

		Point toolBarSize = box.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		toolBarSize.x = 100;
		coolItem.setMinimumSize(toolBarSize);
		coolItem.setPreferredSize(toolBarSize);
		coolItem.setSize(toolBarSize);
	}

	/**
	 * Create the main composite of the bar.
	 * 
	 * @param parent The parent composite.
	 * 
	 * @return The main composite of the bar.
	 */
	private Control createBarComposite(Composite parent) {
		// containing composite
		Composite top = new Composite(parent, SWT.NONE);

		// associated layout
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.verticalSpacing = 0;
		layout.horizontalSpacing = 0;
		layout.numColumns = 3;
		top.setLayout(layout);

		// navigation bar
		ToolBar navigationToolBar = new ToolBar(top, SWT.HORIZONTAL);

		ToolItem backToolItem = new ToolItem(navigationToolBar, SWT.PUSH);
		backToolItem.setToolTipText(Messages.getString("AddressBarContributionItem.back")); //$NON-NLS-1$
		backToolItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_BACK));
		backToolItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// go back in the history
				uiController.getDomain().getCurrentExplorerTab().goBack();

				// update folderView
				uiController.getDomain().getCurrentExplorerTab().getFolderView().update();

				// update folderTreeView
				uiController.getUi().getFolderTreeView().update();
			}
		});

		ToolItem forwardToolItem = new ToolItem(navigationToolBar, SWT.PUSH);
		forwardToolItem.setToolTipText(Messages.getString("AddressBarContributionItem.forward")); //$NON-NLS-1$
		forwardToolItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_FORWARD));
		forwardToolItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// go back in the history
				uiController.getDomain().getCurrentExplorerTab().goNext();

				// update folderView
				uiController.getDomain().getCurrentExplorerTab().getFolderView().update();

				// update folderTreeView
				uiController.getUi().getFolderTreeView().update();
			}
		});

		ToolItem upToolItem = new ToolItem(navigationToolBar, SWT.PUSH);
		upToolItem.setToolTipText(Messages.getString("AddressBarContributionItem.up")); //$NON-NLS-1$
		upToolItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_UP));
		upToolItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ExplorerTab currentExplorerTab = uiController.getDomain().getCurrentExplorerTab();
				File parentFolder = currentExplorerTab.getFolder().getParentFile();
				if (parentFolder != null) {
					currentExplorerTab.navigate(parentFolder);
					currentExplorerTab.getFolderView().update();
					uiController.getUi().getFolderTreeView().update();
				}
			}
		});

		ToolItem refreshToolItem = new ToolItem(navigationToolBar, SWT.PUSH);
		refreshToolItem.setToolTipText(Messages.getString("AddressBarContributionItem.refresh")); //$NON-NLS-1$
		refreshToolItem.setImage(new Image(parent.getDisplay(), Thread.currentThread().getContextClassLoader().getResourceAsStream("org/o14x/alpha/ui/images/refresh.gif"))); //$NON-NLS-1$
		refreshToolItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				uiController.getDomain().getCurrentExplorerTab().getFolderView().update();
				uiController.getUi().getFolderTreeView().update();

				//				TreeViewer treeViewer = uiController.getDomain().getCurrentExplorerTab().getExplorerTreeViewer();
				//				FolderTreeInput folderTreeInput = (FolderTreeInput)treeViewer.getInput();
				//				folderTreeInput.refresh();
				//				treeViewer.refresh();
			}
		});

		// adressCombo
		addressCombo = new Combo(top, SWT.SINGLE);
		addressCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		addressCombo.addKeyListener(new KeyAdapter() {

			/* (non-Javadoc)
			 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
			 */
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode == 13) {
					setCurrentDirectory();
				}
			}

		});

		// OK button
		ToolBar okToolBar = new ToolBar(top, SWT.HORIZONTAL);
		ToolItem okToolItem = new ToolItem(okToolBar, SWT.PUSH);
		okToolItem.setToolTipText(Messages.getString("AddressBarContributionItem.ok")); //$NON-NLS-1$
		Image okImage = new Image(parent.getDisplay(), Thread.currentThread().getContextClassLoader().getResourceAsStream("org/o14x/alpha/ui/images/nav_go.gif")); //$NON-NLS-1$
		okToolItem.setImage(okImage);
		okToolItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setCurrentDirectory();
			}
		});

		return top;
	}

	/**
	 * Sets the current directory with the path entered in the adress bar.
	 */
	private void setCurrentDirectory() {
		uiController.getDomain().getCurrentExplorerTab().navigate(new File(addressCombo.getText()));
		uiController.getUi().getFolderTreeView().update();
		uiController.getDomain().getCurrentExplorerTab().getFolderView().update();
	}

	/**
	 * Show the path of the current directory in the address bar.
	 */
	public void update() {
		addressCombo.setText(uiController.getDomain().getCurrentExplorerTab().getFolder().getPath());
	}
}